package meituan;

import util.MyUtil;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Created by azure on 2017/12/11.
 */
public class MeituanTokenEncrypt
{
    private static String rohr = "var pako = require(\"pako\"); var btoa = require(\"btoa\"); var querystring = require(\"querystring\"); if (!Object.keys) { Object.keys = require(\"object-keys\") }; if (!Function.prototype.bind) { Function.prototype.bind = function(oThis) { if (typeof this !== \"function\") { throw new TypeError(\"Function.prototype.bind - what is trying to be bound is not callable\") }; var aArgs = Array.prototype.slice.call(arguments, 1); var fToBind = this; var fNOP = function() {}; var fBound = function() { return fToBind.apply(this instanceof fNOP && oThis ? this: oThis, aArgs.concat(Array.prototype.slice.call(arguments))) }; fNOP.prototype = this.prototype; fBound.prototype = new fNOP(); return fBound } }; if (typeof Array.prototype.forEach !== \"function\") { Array.prototype.forEach = function(callback, context) { for (var i = 0; i < this.length; i++) { callback.apply(context, [this[i], i, this]) } } }; if (typeof JSON === \"undefined\") { JSON = require(\"json3\") }; var getBrowserViewportDimensions = function() { var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); return [w, h] }; var getBrowserResolution = function() { var totalSize = [screen.width, screen.height]; var availableSize = [screen.availWidth, screen.availHeight]; var colorDepth = screen.colorDepth; var colorResolution = screen.pixelDepth; return [totalSize, availableSize, colorDepth, colorResolution] }; var getAutomate = function() { if (window._phantom || window.phantom || window.callPhantom) { return \"ps\" }; return \"\" }; var getBaseInfo = function() { var referer = document.referrer; var location = window.location.href; return [location, referer] }; var encryptContent = function(content) { content = pako.deflate(JSON.stringify(content), { to: \"string\" }); content = btoa(content); return content }; var generateUrlSignatureByParams = function(params) { var concatenated = []; var keys = Object.keys(params).sort(); keys.forEach(function(item, index) { if (item !== \"token\" && item !== \"_token\") { concatenated.push(item + \"=\" + params[item]) } }); concatenated = concatenated.join(\"&\"); return encryptContent(concatenated) }; var rohr = { rId: Rohr_Opt.Flag, ver: \"1.0.5\", ts: new Date().getTime(), cts: new Date().getTime(), brVD: getBrowserViewportDimensions(), brR: getBrowserResolution(), bI: getBaseInfo(), mT: [], kT: [], aT: [], tT: [], aM: getAutomate() }; rohr.bindUserTrackEvent = function() { var handleMouseMove = function(event) { var eventDoc, doc, body; event = event || window.event; if (event.pageX == null && event.clientX !== null) { eventDoc = (event.target && event.target.ownerDocument) || document; doc = eventDoc.documentElement; body = eventDoc.body; event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0) }; this.mT.unshift([event.pageX, event.pageY].join(\",\")); if (this.mT.length > 30) { this.mT = this.mT.slice(0, 30) } }.bind(this); var handleKeyboardPress = function(event) { event = event || window.event; var charCode = typeof event.which === \"number\" ? event.which: event.keyCode; if (charCode) { if (!event[\"srcElement\"]) { event.srcElement = event.target }; this.kT.unshift([String.fromCharCode(charCode), event.srcElement.nodeName].join(\",\")) }; if (this.kT.length > 30) { this.kT = this.kT.slice(0, 30) } }.bind(this); var handleTouchMove = function(event) { var eventDoc, doc, body, pageX, pageY; if (event.touches[0].clientX !== null) { eventDoc = (event.target && event.target.ownerDocument) || document; doc = eventDoc.documentElement; body = eventDoc.body; pageX = event.touches[0].clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); pageY = event.touches[0].clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0) }; this.tT.unshift([pageX, pageY, event.touches.length].join(\",\")); if (this.tT.length > 30) { this.tT = this.tT.slice(0, 30) } }.bind(this); var handleManrohrlAction = function(event) { event = event || window.event; if (!event[\"srcElement\"]) { event.srcElement = event.target }; this.aT.unshift([event.clientX, event.clientY, event.srcElement.nodeName].join(\",\")); if (this.aT.length > 30) { this.aT = this.aT.slice(0, 30) } }.bind(this); function bindEvent(evnt, elem, func, phase) { if (elem.addEventListener) { elem.addEventListener(evnt, func, phase || false) } else { if (elem.attachEvent) { elem.attachEvent(\"on\" + evnt, func) } else { elem[evnt] = func } } } bindEvent(\"mousemove\", document, handleMouseMove, true); bindEvent(\"keydown\", document, handleKeyboardPress, true); bindEvent(\"click\", document, handleManrohrlAction, true); if (\"ontouchmove\" in document) { bindEvent(\"touchmove\", document, handleTouchMove, true) } }; rohr.reload = function(requestUrl) { var rohrdata; var urlParams = {}; if (typeof requestUrl === \"string\") { urlParams = querystring.parse(requestUrl.split(\"?\")[1]) } else { if (typeof requestUrl === \"object\") { urlParams = requestUrl } }; rohr.sign = generateUrlSignatureByParams(urlParams); rohr.cts = new Date().getTime(); rohrdata = encryptContent(rohr); if (Rohr_Opt.LogVal && typeof(window) !== \"undefined\") { window[Rohr_Opt.LogVal] = encodeURIComponent(rohrdata) }; return rohrdata }; if (typeof(Rohr_Opt) === \"object\") { rohr.bindUserTrackEvent(); Rohr_Opt.reload = rohr.reload; Rohr_Opt.sign = rohr.sign; Rohr_Opt.clean = rohr.decrypt }";
    public static String encrypter()
    {
        ScriptEngineManager manager = new ScriptEngineManager();
        String ret = "";
        String jsEncryptor = MyUtil.readString("mt.js");
        ScriptEngine engine = manager.getEngineByName("js");
        String para = "classify_type=cate_all&sort_type=0&price_type=0&support_online_pay=0&support_invoice=0&support_logistic=0&page_offset=141&page_size=20&uuid=rrQrg_mCHFymT834xzhag9RDAVEEmYNpgEeE2UOEWw4TR1B15OKmCOxF2aqVcHiz&platform=1&partner=4&originUrl=http%3A%2F%2Fwaimai.meituan.com%2Fhome%2Fwtw3rnfw1djv%3Fstart%3D10";
        try {
            engine.eval(rohr);
            Invocable inv = (Invocable) engine;
            ret = (String)inv.invokeFunction("Rohr_Opt.reload", "http://waimai.meituan.com/ajax/poilist?" + para);
            System.out.println(ret);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ret;
    }
}
